<?php
	/**
	 * Elgg Event category - add action
	 * 
	 * @package Elgg Event Manager
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Produce Here
	 * @copyright Produce Here 2011
	 * @link 
	 * 
	 * omitted product ID
	 **/

	global $CONFIG;
	
	// Get variables

	$title = trim(get_input("categorytitle"));
	//$product_type_id = trim(get_input("product_type_id"));  //------------
	$desc = get_input("categorybody");
	$container_guid = (int) get_input('container_guid', 0);
	if (!$container_guid){
		$container_guid = $_SESSION['user']->getGUID();
	}
	
	//Validation
	if(empty($title)){
		$error_field = elgg_echo("title");
	}
	//if(empty($product_type_id) || $product_type_id <=0){ 	//------------
	//	$error_field = elgg_echo("product:type");			//------------
	//}
	//$product_type_id = 2;									// digital products fix --- temporary
	
	if(!empty($error_field)){
		$_SESSION['eventcategory']['eventcategorytitle'] = $title;
		$_SESSION['eventcategory']['eventcategorybody'] = $desc;
	//	$_SESSION['eventcategory']['product_type_id'] = $product_type_id;	//------------
		
		register_error(sprintf(elgg_echo("product:validation:null"),$error_field));
		$container_user = get_entity($container_guid);
		$redirect = $CONFIG->wwwroot . "pg/event_manager/" . $container_user->username . "/addCatEve/";
	}else{
		// Extract categories from, save to default social commerce (for now)
		$category = new ElggObject();
		
              $category->access_ed = 1;
		$category->access_id = 2;
		$category->subtype="categoryevent";
		$category->title = $title;
		//$category->product_type_id = $product_type_id;	//------------
		$category->description = $desc;
		
		if ($container_guid){
			$category->container_guid = $container_guid;
		}
		
		$result = $category->save();
		
		if ($result){
			system_message(elgg_echo("category:saved"));
			unset($_SESSION['category']);
		}else{
			register_error(elgg_echo("category:uploadfailed"));
		}
			
		$container_user = get_entity($container_guid);
		//$redirect = $CONFIG->wwwroot . "pg/event_manager/" . $container_user->username . "/category/";
	}
	
	//forward($redirect);
        forward(REFERER);
?>
